package com.forlove.app.receiver;

import android.app.NotificationManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import com.forlove.app.common.network.ICallBack;
import com.forlove.app.common.utils.LogUtil;
import com.forlove.app.common.utils.StringUtils;
import com.forlove.app.manager.NetworkManager;
import com.forlove.app.manager.PushManager;
import com.forlove.app.manager.ServerManager;
import com.forlove.app.manager.UserManager;
import com.forlove.app.qselect.MainActivity;
import com.forlove.app.qselect.index.ui.IndexFragment;
import com.forlove.app.receiver.entity.PushPathEntity;
import com.forlove.app.splash.WelcomeActivity;

import java.util.HashMap;
import java.util.Map;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by yjl on 2017/11/2.
 */

public class QuanReceiver extends BroadcastReceiver {
    private static final String TAG = "QuanReceiver";

    private NotificationManager nm;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null == nm) {
            nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        }

        Bundle bundle = intent.getExtras();
//        LogUtil.d(TAG, "onReceive - " + intent.getAction() + ", extras: " + AndroidUtil.printBundle(bundle));

        if (context != null && JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            LogUtil.d(TAG, "JPush用户注册成功");
            String registerId = JPushInterface.getRegistrationID(context);
            LogUtil.e(TAG, "jspush registerid:" + registerId);
            UserManager.saveJPUSHId(context, registerId);
            upRegisterId(context, registerId);
        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            LogUtil.d(TAG, "接受到推送下来的自定义消息");

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            LogUtil.d(TAG, "接受到推送下来的通知");

            receivingNotification(context, bundle);

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            LogUtil.d(TAG, "用户点击打开了通知");
            openNotification(context, bundle);

        } else {
            LogUtil.d(TAG, "Unhandled intent - " + intent.getAction());
        }
    }


    private void receivingNotification(Context context, Bundle bundle) {
        String title = bundle.getString(JPushInterface.EXTRA_NOTIFICATION_TITLE);
        LogUtil.d(TAG, " title : " + title);
        String message = bundle.getString(JPushInterface.EXTRA_ALERT);
        LogUtil.d(TAG, "message : " + message);
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        LogUtil.d(TAG, "extras : " + extras);
    }

    private void openNotification(Context context, Bundle bundle) {
        if (bundle == null || context == null) {
            return;
        }
        String extras = bundle.getString(JPushInterface.EXTRA_EXTRA);
        LogUtil.e("yjl", "jpush EXTRA_EXTRA:" + extras);
        if (StringUtils.isNotEmpty(extras)) {
            PushPathEntity pushPathEntity = new PushPathEntity();
            pushPathEntity.parseJSONString(extras);

            Intent intent = new Intent(context, MainActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            if (PushManager.OPEN_PATH.equals(pushPathEntity.path)) {
                // 打开到我的魅力
                bundle.putInt("position", MainActivity.POSITION_INDEX);
                bundle.putInt("childView", IndexFragment.POSITION_INDEX_MiNE);
            } else if (PushManager.OPEN_FRIEND.equals(pushPathEntity.path)) {
                //打开好友
                bundle.putInt("position", MainActivity.POSITION_FRIEND);
            } else if (PushManager.OPEN_CIRCLE.equals(pushPathEntity.path)) {
                //圈题
                bundle.putInt(MainActivity.Key_Comefrom, MainActivity.COME_TO_CIRCLE);
            }
            intent.putExtras(bundle);
            context.startActivity(intent);
        } else {
            //收到推送如果没有路径就直接打开应用
            Intent intent = new Intent(context, WelcomeActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
//            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
            context.startActivity(intent);
        }

    }

    private void upRegisterId(Context context, String regId) {
        if (context != null && UserManager.getUserLoginState(context)) {
            Map param = new HashMap();
            param.put("regId", regId);
            try {
                NetworkManager.getInstance().loadDataByPost(ServerManager.ACTION_UP_REGISTERID, param, new ICallBack() {
                    @Override
                    public void onSuccess(int statusCode, String result) {
                        LogUtil.e("yjl", "上传jpush id:" + result);
                    }

                    @Override
                    public void onFail(int statusCode, String result) {
                        LogUtil.e("yjl", "上传jpush id:" + result);
                    }
                });
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
